﻿// 根据用户输入的数字，输出其质因数分解结果

using System;

namespace PrimeFactorization
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("请输入一个大于1的正整数：");
                string input = Console.ReadLine();
                int num;
                if (int.TryParse(input, out num))
                {
                    if (num <= 1)
                    {
                        Console.WriteLine("输入数据不符合要求！");
                        Console.WriteLine(
                            "---------------------------------------------"
                        );
                        continue;
                    }
                    Console.WriteLine("质因数分解结果为：");
                    PrimeFactorization(num);
                }
                else
                {
                    Console.WriteLine("输入数据不符合要求！");
                }
                Console.WriteLine(
                    "---------------------------------------------"
                );
            }
            
        }

        // 判断一个数是否为质数
        static bool isPrime(int num)
        {
           if (num < 2)
           {
               return false;
           }
           for (int i = 2; i <= Math.Sqrt(num); i++)
           {
              if (num % i == 0)
              {
                 return false;
              }
           }
           return true;
        }

        // 分解质因数
        static void PrimeFactorization(int num)
        {
            if (isPrime(num))
            {
                Console.WriteLine(num);
                return;
            }
            for (int i = 2; i <= num; i++)
            {
                if (num % i == 0)
                {
                    Console.WriteLine(i);
                    PrimeFactorization(num / i);
                    break;
                }
            }
        }
    }
}